package pl.edu.pw.polygen.ui.file;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import pl.edu.pw.polygen.beans.FileBean;
import pl.edu.pw.polygen.beans.ProjectBean;
import pl.edu.pw.polygen.core.file.FileService;
import pl.edu.pw.polygen.core.project.ProjectService;
import pl.edu.pw.polygen.ui.explorer.ProjectExplorer;
import pl.edu.pw.polygen.util.ApplicationHelper;

import com.vaadin.ui.AbstractLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@Component
@Scope("prototype")
public class FilePropertiesWindow extends Window {

	private static final long serialVersionUID = 3439452761531103410L;
	private static final String windowName = "File properties";

	private Logger LOG = Logger.getLogger(FilePropertiesWindow.class);
	
	@Autowired
	private FileService fileService;
	
	@Autowired
	private FileForm fileForm;
	
	@Autowired
	private EditorPanelFactory editorPanelFactory;
	
	@Autowired
	private ProjectExplorer projectExplorer;
	
	@Autowired
	private ProjectService projectService;
	
	
	private FileBean fileBean;
	private FilePropertiesWindow instance;
	private Button okButton;
	private Button cancelButton;
	private HorizontalLayout buttonsLayout;

	public FilePropertiesWindow() {
		super(windowName);
		instance = this;
	}

	@SuppressWarnings("unused")
	@PostConstruct
	private void initialize() {
		setModal(true);
		setResizable(false);
		VerticalLayout layout = (VerticalLayout) this.getContent();
		layout.setMargin(true);
		layout.setSpacing(true);
		layout.setSizeUndefined();

		layout.addComponent((AbstractLayout) fileForm);
		layout.addComponent(getButtonsLayout());
		layout.setComponentAlignment(getButtonsLayout(), Alignment.BOTTOM_RIGHT);
	}
	
	public void initialize(String fileName,String projectName) {
		init();
		Long userId = ApplicationHelper.getLoggedUser().getId();
		
		fileBean = fileService.findFile(fileName, projectName, userId);
		if(fileBean!=null){
			fileForm.setFileName(fileBean.getFileName());
			fileForm.setFileDescription(fileBean.getDescription());
			fileForm.setProjectName(fileBean.getProject().getName());
		}
	}

	private HorizontalLayout getButtonsLayout() {
		if(buttonsLayout==null){
			buttonsLayout = new HorizontalLayout();
			buttonsLayout.setSpacing(true);
			buttonsLayout.addComponent(getOkButton());
			buttonsLayout.addComponent(getCancelButton());
		}
		return buttonsLayout;
	}

	private Button getOkButton() {
		if (okButton == null) {
			okButton = new Button("Save");
			okButton.addListener(new ClickListener() {

				private static final long serialVersionUID = 982663756216569796L;

				@Override
				public void buttonClick(ClickEvent event) {
					if(fileForm.isValid()){
						String fileName = fileForm.getFileName();
						String fileDescription = fileForm.getFileDescription();
						Long projectId = fileForm.getProjectId();
						
						FileBean oldFileBean = new FileBean();
						oldFileBean.setFileName( fileBean.getFileName());
						oldFileBean.setDescription(fileBean.getDescription());
						ProjectBean oldProjectBean = fileBean.getProject();
						oldProjectBean.setId(projectId);
						oldFileBean.setProject(oldProjectBean);
						
						ProjectBean projectBean = projectService.findProject(projectId);
						fileBean.setProject(projectBean);
						fileBean.setFileName(fileName);
						fileBean.setDescription(fileDescription);
						fileBean.getProject().setId(projectId);
						
						fileBean = fileService.saveFile(fileBean);
						
						projectExplorer.renameFile(oldFileBean, fileBean);
						(instance.getParent()).removeWindow(instance);
						ApplicationHelper.showNotification("Save file", "File '" + fileName +"' has been saved successfully.");
					}
				}
			});
			okButton.setWidth("80px");
		}
		return okButton;
	}

	private Button getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new Button("Cancel");
			cancelButton.addListener(new ClickListener() {

				private static final long serialVersionUID = 6840259256005300430L;

				@Override
				public void buttonClick(ClickEvent event) {
					(instance.getParent()).removeWindow(instance);
				}
			});
			cancelButton.setWidth("80px");
		}
		return cancelButton;
	}
	
	public void init(){
		fileForm.initialize();
		fileForm.setFileName("");
		fileForm.setFileDescription("");
	}
	
	public void init(String projectName){
		fileForm.initialize();
		fileForm.setProjectName(projectName);
		fileForm.setFileName("");
		fileForm.setFileDescription("");
	}

	

}
